package logic

import (
	"context"
	"manuscriptservice/manuscriptservicerpc/internal/svc"
	"manuscriptservice/manuscriptservicerpc/manuscriptservicerpc"
	"manuscriptservice/model/mysql"
	"strconv"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *manuscriptservicerpc.AddRequest) (*manuscriptservicerpc.AddResponse, error) {
	// todo: add your logic here and delete this line
	var manu mysql.Good

	manu = mysql.Good{
		Title:   in.Title,
		Time:    in.Time,
		Image:   in.Image,
		Stock:   in.Stock,
		ClassId: in.ClassID,
	}

	err := manu.FindManuList(mysql.DB, in.Title)
	if err != nil {

		return nil, err
	}

	err = manu.ManuAdd(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &manuscriptservicerpc.AddResponse{
		ManuId: strconv.Itoa(int(manu.ID)),
	}, nil
}
